#include "MainFrame/MenuBar/Dialogs/BrightnessContrastDialog.h"
#include <QBoxLayout>
#include <QPushButton>

#include "Colors/RIVIABrightnessContrast.h"
#include "MainFrame/CentralWidget/CentralManager.h"

#include <QSlider>
#include <QEvent>
#include <QLabel>

BrightnessContrastDialog::BrightnessContrastDialog(QWidget *parent, Qt::WindowFlags f):
    QDialog(parent, f),
    m_pBrightnessLabel(nullptr),
    m_pContrastLabel(nullptr),
    m_pGammaLabel(nullptr),
    m_pBrightnessSlider(nullptr),
    m_pContrastSlider(nullptr),
    m_pGammaSlider(nullptr),
    m_pConfirmButton(nullptr),
    m_nBrightness(0),
    m_nContrast(1),
    m_nGamma(1)
{
    initDialog();
    connetSignalToSlots();
    initTrans();
}

BrightnessContrastDialog::~BrightnessContrastDialog()
{

}

void BrightnessContrastDialog::initDialog()
{
    QVBoxLayout* layout = new QVBoxLayout(this);
    this->setLayout(layout);

    m_pBrightnessLabel = new QLabel(this);
    m_pContrastLabel = new QLabel(this);
    m_pGammaLabel = new QLabel(this);
    m_pBrightnessSlider = new QSlider(Qt::Horizontal, this);
    m_pContrastSlider = new QSlider(Qt::Horizontal, this);
    m_pGammaSlider = new QSlider(Qt::Horizontal, this);
    m_pBrightnessSlider->setRange(1, 200);
    m_pBrightnessSlider->setValue(100);
    m_pContrastSlider->setRange(1, 500);
    m_pContrastSlider->setValue(100);
    m_pGammaSlider->setRange(1, 200);
    m_pGammaSlider->setValue(100);

    layout->addWidget(m_pBrightnessLabel);
    layout->addWidget(m_pBrightnessSlider);
    layout->addWidget(m_pContrastLabel);
    layout->addWidget(m_pContrastSlider);
    layout->addWidget(m_pGammaLabel);
    layout->addWidget(m_pGammaSlider);

    m_pConfirmButton = new QPushButton(this);
    layout->addWidget(m_pConfirmButton);
}

void BrightnessContrastDialog::connetSignalToSlots()
{
    connect(m_pBrightnessSlider, SIGNAL(valueChanged(int)), this, SLOT(changeBrightnessSlot(int)));
    connect(m_pContrastSlider, SIGNAL(valueChanged(int)), this, SLOT(changeContrastSlot(int)));
    connect(m_pGammaSlider, SIGNAL(valueChanged(int)), this, SLOT(changeGammaSlot(int)));
    connect(m_pConfirmButton, SIGNAL(pressed()), this, SLOT(confirmSlot()));
}

void BrightnessContrastDialog::changeEvent(QEvent *event)
{
    QWidget::changeEvent(event);
    switch (event->type())
    {
    case QEvent::LanguageChange:
        initTrans();
        break;
    default:
        break;
    }
}

void BrightnessContrastDialog::initTrans()
{
    this->setWindowTitle(tr("Brightness and Contrast"));
    m_pBrightnessLabel->setText(tr("Brightness: ") + "0");
    m_pContrastLabel->setText(tr("Constrast: ") + "1");
    m_pGammaLabel->setText(tr("Gamma: ") + "1");

    m_pConfirmButton->setText(tr("Confirm"));
}

void BrightnessContrastDialog::execBrightnessContrast()
{
    RIVIABrightnessContrast().changeBrightnessContrast(m_nContrast, m_nBrightness, m_nGamma);
    CentralManager::updateImageViewWidget();
}

void BrightnessContrastDialog::changeBrightnessSlot(int value)
{
    m_nBrightness = value - 100;
    m_pBrightnessLabel->setText(tr("Brightness: ") + QString::number(m_nBrightness));
    execBrightnessContrast();
}

void BrightnessContrastDialog::changeContrastSlot(int value)
{
    m_nContrast = value / 100.0;
    m_pContrastLabel->setText(tr("Constrast: ") + QString::number(m_nContrast));
    execBrightnessContrast();
}

void BrightnessContrastDialog::changeGammaSlot(int value)
{
    m_nGamma = value / 100.0;
    m_pGammaLabel->setText(tr("Gamma: ") + QString::number(m_nGamma));
    execBrightnessContrast();
}

void BrightnessContrastDialog::confirmSlot()
{
    RIVIABrightnessContrast().finishChange();
    this->close();
}
